package com.hitqz.robot.biz.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hitqz.robot.api.business.model.param.patrol.QueryPatrolObjectParams;
import com.hitqz.robot.api.business.model.vo.patrol.PatrolObjectStandardVo;
import com.hitqz.robot.api.business.model.vo.patrol.PatrolObjectVo;
import com.hitqz.robot.api.common.entity.AlarmRule;
import com.hitqz.robot.api.common.entity.PatrolCheckType;
import com.hitqz.robot.api.common.entity.PatrolObject;
import com.hitqz.robot.api.common.entity.PatrolObjectStandard;
import com.hitqz.robot.biz.service.AlarmRuleService;
import com.hitqz.robot.biz.service.PatrolCheckTypeService;
import com.hitqz.robot.biz.service.PatrolObjectService;
import com.hitqz.robot.biz.service.PatrolObjectStandardService;
import com.hitqz.robot.common.core.util.R;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author wengyx
 * @date 2024/8/7
 */

@RestController
@AllArgsConstructor
@RequestMapping("/patrolObject")
@Tag(name = "巡检对象管理")
public class PatrolObjectController {

    private final PatrolObjectService patrolObjectService;

    private final PatrolObjectStandardService patrolObjectStandardService;

    private final PatrolCheckTypeService patrolCheckTypeService;

    private final AlarmRuleService alarmRuleService;

    @PostMapping("/list")
    @Operation(summary = "获取巡检对象列表")
    public R<List<PatrolObjectVo>> list(@RequestBody(required = false) QueryPatrolObjectParams query) {
        List<PatrolObject> data = patrolObjectService.list(Wrappers.<PatrolObject>lambdaQuery()
                .eq(query.getPatrolCheckTypeId()!=null,PatrolObject::getPatrolCheckTypeId, query.getPatrolCheckTypeId())
                .orderBy(true,false, PatrolObject::getPatrolCheckTypeId));
        List<PatrolObjectVo> voList = BeanUtil.copyToList(data, PatrolObjectVo.class);
        if (query.getPatrolCheckTypeId()!=null){
            PatrolCheckType patrolCheckType = patrolCheckTypeService.getById(query.getPatrolCheckTypeId());
            voList.forEach(f->f.setResultType(patrolCheckType.getResultType()));
        }
        return R.ok(voList);
    }

    @PostMapping("/page")
    @Operation(summary = "分页获取巡检对象")
    public R<Page<PatrolObjectVo>> page(@RequestBody QueryPatrolObjectParams query) {
        Page page = new Page<>(query.getCurrent(),query.getSize());
        Page<PatrolObject> data = patrolObjectService.page(page,
                Wrappers.<PatrolObject>lambdaQuery()
                        .eq(query.getPatrolCheckTypeId()!=null, PatrolObject::getPatrolCheckTypeId,
                                query.getPatrolCheckTypeId())
                        .orderByDesc(PatrolObject::getId));
        Page<PatrolObjectVo> voPage = new Page<>();
        BeanUtil.copyProperties(data,voPage);
        List<PatrolObjectVo> voList = BeanUtil.copyToList(data.getRecords(), PatrolObjectVo.class);
        voPage.setRecords(voList);
        if (query.getPatrolCheckTypeId()!=null){
            PatrolCheckType patrolCheckType = patrolCheckTypeService.getById(query.getPatrolCheckTypeId());
            voPage.getRecords().forEach(f->f.setResultType(patrolCheckType.getResultType()));
        }
        return R.ok(voPage);
    }

    @GetMapping("/{id}" )
    @Operation(summary = "根据id查询巡检对象")
    public R<PatrolObjectVo> getById(@PathVariable("id") Integer id) {
        PatrolObject patrolObject = patrolObjectService.getById(id);
        PatrolObjectVo vo = new PatrolObjectVo();
        BeanUtil.copyProperties(patrolObject,vo,false);
        PatrolCheckType patrolCheckType = patrolCheckTypeService.getById(patrolObject.getPatrolCheckTypeId());
        if (patrolCheckType!=null){
            vo.setResultType(patrolCheckType.getResultType());
        }
        return R.ok(vo);
    }


    @GetMapping("/{id}/patrolStandard")
    @Operation(summary = "根据id查询巡检对象底下的标准值")
    public R<PatrolObjectStandardVo> getPatrolObjectStandardById(@PathVariable(value = "id") Integer id) {
        if (id==null){
            return R.failed("巡检对象id不能为空");
        }
        PatrolObjectStandard patrolObjectStandard = patrolObjectStandardService.getOne(Wrappers.<PatrolObjectStandard>lambdaQuery()
                .eq( PatrolObjectStandard::getPatrolObjectId, id).last("limit 1"));
        PatrolObjectStandardVo vo = new PatrolObjectStandardVo();
        if(patrolObjectStandard==null) {
            //保存一条默认的
            patrolObjectStandard = new PatrolObjectStandard();
            patrolObjectStandard.setPatrolObjectId(id);
            patrolObjectStandardService.save(patrolObjectStandard);
        }
        BeanUtil.copyProperties(patrolObjectStandard, vo, false);
        List<AlarmRule> ruleList=alarmRuleService.list(Wrappers.<AlarmRule>lambdaQuery()
                .eq(AlarmRule::getStandardId, patrolObjectStandard.getId()));
        vo.setRuleList(ruleList);
        return R.ok(vo);
    }

    @PostMapping
    @Operation(summary = "新增巡检对象")
    public R save(@RequestBody @Validated PatrolObject dto) {
        return R.ok(patrolObjectService.save(dto));
    }

    @PutMapping
    @Operation(summary = "修改巡检对象")
    public R updateById(@RequestBody @Validated PatrolObject dto) {
        return R.ok(patrolObjectService.updateById(dto));
    }

    @DeleteMapping
    @Operation(summary = "根据id删除巡检对象")
    public R removeById(@RequestBody Integer[] ids) {
        patrolObjectStandardService.remove(Wrappers.<PatrolObjectStandard>lambdaQuery().in(PatrolObjectStandard::getPatrolObjectId, ids));
        return R.ok(patrolObjectService.removeBatchByIds(CollUtil.toList(ids)));
    }

}
